<template>
    <div class="user">
        <van-nav-bar title="可抢商品" left-text="" right-text="" left-arrow @click-left="onClickLeft">
            <template #left>
                <van-icon name="cross" size="19" color="#000" />
            </template>
            <!-- <template #right>
                <van-icon name="ellipsis" size="22" color="#000" />
            </template> -->
        </van-nav-bar>
        <div>
            <div class="main" v-if="goodsList.length > 0">
                <div v-for="(item, idx) in goodsList" :key="idx" class="goods-item">
                    <div class="pic">
                        <image-pic fill="cover" align="center" width="180" height="180" :src="item.image" />
                    </div>
                    <p v-if="(item.end - (nowdata / 1000)) > 0" class="title2"
                        style="font-family: MiSans, MiSans;font-weight: 400;font-size: 13px;color: #666666;display: flex;">
                        倒计时 <van-count-down :time="(item.end - (nowdata / 1000)) * 1000"
                            style="font-family: MiSans, MiSans;font-weight: 400;font-size: 13px;color: #666666" />
                    </p>
                    <p v-else class="title2"
                        style="font-family: MiSans, MiSans;font-weight: 400;font-size: 13px;color: #666666;">
                        倒计时00:00:00
                    </p>
                    <p class="title">{{ item.name }}</p>
                    <div class="num" style="display: flex;">
                        <span class="num__now">¥{{ item.price }}</span>
                        <span class="num__old" @click="qiang(item.id)">抢</span>
                    </div>
                </div>
                <!-- <goods-item v-for="(item, idx) in goodsList" :key="idx" :goods-id="item.id" :img="item.image"
                    :title="item.name" :desc="item.brief" :price="item.price" :discount="item.counterPrice" /> -->
            </div>
            <div v-else>
                <van-list :finished="true" :immediate-check="false">
                    <div class="main2" style="text-align: center;height: 200px;">
                        <img src="@/assets/homr/Group 9420.png" alt="logo"
                            style="width: 180px;height: 120px;margin-top: 40px;">
                        <div>暂无可抢购商品</div>
                    </div>
                </van-list>
            </div>
        </div>

        <van-popup v-model="showDialog" :style="{ height: '190px', width: '300px', borderRadius: '15px' }">
            <div class="dialog-content" style="text-align: center;">
                <img src="@/assets/homr/Frame.png" alt="" srcset="" style="width: 34px;height: 34px;margin-top: 21px;">
                <div
                    style="font-family: MiSans, MiSans;font-weight: 400;font-size: 15px;color: #333333;margin-top: 14px;">

                </div>
                <div style="font-family: MiSans, MiSans;font-weight: 400;font-size: 15px;color: #333333;">
                    {{ massage }}
                </div>
                <div style="text-align: center;">

                    <div style="width: 234px;height: 44px;background: #389138;border-radius: 100px;line-height: 44px;color: #fff;margin-top: 15px;margin-left: 28px;"
                        @click="queding">确定</div>
                </div>
            </div>
        </van-popup>
    </div>
</template>

<script>

import GoodsItem from '@/components/GoodsItem'
import { getGoodslist } from '@/api/user'
import { Toast } from 'vant';
export default {
    name: 'User',
    components: {
        GoodsItem
    },
    data() {
        return {
            nowdata: new Date().getTime(),
            goodsList: [],
            showDialog: false,
            massage: "",

            tickertwo: null,// 这款里是two是因为前面我也有倒计时
        };
    },
    mounted() {
        this.getRecommonGoodsList()
        //这一段是防止进入页面出去后再进来计时器重复启动
        if (this.tickertwo) {
            clearInterval(this.tickertwo);
        }
        this.beginTimer();
    },
    destroyed() {
        if (this.tickertwo) {
            clearInterval(this.tickertwo);
        }
    },
    methods: {
        secondsToTime(seconds) {
            const hours = Math.floor(seconds / 3600);
            const minutes = Math.floor((seconds % 3600) / 60);
            const remainingSeconds = seconds % 60;
            let h;
            let m;
            let s;
            if (hours < 10) {
                h = "0" + hours
            } else {
                h = hours
            }
            if (minutes < 10) {
                m = "0" + minutes
            } else {
                m = minutes
            }
            if (remainingSeconds < 10) {
                s = "0" + remainingSeconds
            } else {
                s = remainingSeconds
            }
            return h + ":" + m + ":" + s;
        },

        beginTimer() { //这个计时器是每秒减去数组中指定字段的时间
            let vm = this
            this.tickertwo = setInterval(() => {
                //数据循环，因为要每一列数据都要倒计时，所以要对每一列的数据进行处理
                for (let i = 0, len = vm.goodsList.length; i < len; i++) {
                    const item = vm.goodsList[i];
                    item.create_time = item.create_time + 1;

                }
            }, 1000);
        },
        qiang(id) {
            let obj = {
                ids: id,
                type: 1
            }
            addorder(obj).then((res) => {
                if (res.code == 1) {
                    this.massage = res.msg
                    this.getRecommonGoodsList()
                } else {
                    Toast(res.msg)
                }

            })
        },
        queding() {
            this.showDialog = false
        },
        onClickLeft() {
            this.$router.back()
        },
        getRecommonGoodsList() {
            let obj = {
                page: 1,
                pageSize: 100
            }
            getGoodslist(obj).then(res => {
                if (res.code == 1) {
                    this.goodsList = res.data.list
                }

            })
        },
    }

}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

.goods-item {
    position: relative;
    width: 360px;
    overflow: hidden;

    .pic {
        display: block;
        padding: 14px;
        border-radius: 8px;
        // background: #f5f5f5;
    }

    .title {
        padding-left: 14px;
        padding-top: 0px !important;
        // font-size: $small;
        // color: $black;
        width: 320px;
        height: 80px;
        font-family: MiSans, MiSans;
        font-weight: 400;
        font-size: 30px;
        color: #151515;
        line-height: 40px;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: normal;
        // text-align: center;
        // @include text-ellipsis;
    }

    .title2 {
        padding-left: 14px;
        padding-top: 0px !important;
        // font-size: $small;
        // color: $black;
        width: 320px;
        height: 40px;
        font-family: MiSans, MiSans;
        font-weight: 400;
        font-size: 30px;
        color: #151515;
        line-height: 40px;
        // text-align: center;
        // @include text-ellipsis;
    }


    .desc {
        padding: 14px;
        padding-top: 0;
        font-size: $small;
        color: $gray;
        text-align: center;
        @include text-ellipsis;
    }

    .num {
        padding-bottom: 14px;
        // text-align: center;
        padding: 14px;
        // font-size: $small;
        color: #2E512E;
        display: inline-block;

        .num__now {
            font-family: DIN-Medium, DIN;
            font-size: 34px;
            font-weight: 600;
            margin-right: 10px;
            width: 122px;
        }

        .num__old {
            font-family: DIN-Medium, DIN;
            // font-size: 24px;
            font-weight: 400;
            // color: $gray;
            // text-decoration: line-through;
            margin-right: 10px;
            float: right;
            width: 100px;
            height: 52px;
            background: #389138;
            border-radius: 24px 0px 0px 24px;
            margin-left: 100px;
            text-align: center;
            font-size: 32px;
            color: #FFFFFF;
            justify-content: center;
            line-height: 52px;

        }
    }

    .btn-wrapper {
        display: inline-block;
        display: flex;
        flex-direction: row;
        justify-content: center;
        padding-bottom: 24px;

        .btn {
            background: $red;
            color: #fff;
            border: none;
        }
    }
}

.main {
    background: #fff;
    box-sizing: border-box;
    padding: 0 10px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    flex-wrap: wrap;
}
</style>